package com.example.springboot_demo01.service;

import com.example.springboot_demo01.dao.ScoreDao;
import com.example.springboot_demo01.entity.Classes;
import com.example.springboot_demo01.entity.Score;
import com.example.springboot_demo01.entity.form.ScoreForm;
import com.example.springboot_demo01.entity.form.ScoreItem;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//成绩管理服务类的定义
@Service
public class ScoreService {
    @Resource
    ScoreDao scoreDao;
    //定义批量成绩录入的服务
    // ----将传递过来的数据对应的每个学生的成绩一一保存到成绩表中
    public int add(ScoreForm scoreForm){
        //遍历分数列表中的数据，转成Score对象，进行保存
        for(ScoreItem item: scoreForm.getScoreItemList()){
            Score  obj  = new Score();  //生成Score对象 设置属性
            obj.setCourseId(  scoreForm.getCourseId());
            obj.setStudentId( item.getStudentId());
            obj.setPoint(     item.getPoint());
            scoreDao.insert(obj);  //保存
        }

        return 1;
    }

    public List<Score> getScoreListByClassAndCourse(int classId,
                                                    int courseId){
        return scoreDao.getScoreListByClassAndCourse(classId,courseId);
    }

    public Map getReportByClassAndCourse(int classId,
                                         int courseId){
        Map  result  = new HashMap<String,Object>();

        //1.获得 最高分，最低分，平均分，最大分差
        Map  map1 = scoreDao.getMaxAndMinByClassAndCourse(classId,courseId);
        result.put("maxPoint", map1.get("maxPoint"));
        result.put("minPoint", map1.get("minPoint"));
        result.put("avgPoint", map1.get("avgPoint"));
        result.put("disPoint", map1.get("disPoint"));

        //2.获得及格率
        Map  map2 = scoreDao.getPassRateByClassAndCourse(classId,courseId);
        result.put("passRate", map2.get("passRate"));

        return result;
    }

    //某门课程 各个班级的成绩分析
    public List<Map> getReportByCourse(int courseId){
        List<Map>  result  = new ArrayList<>();
        //1.取该门课所安排的班级信息数组
        List<Classes> classesList = scoreDao.getClassesList(courseId);
        //2.对班级信息数组进行遍历，循环调用getReportByClassAndCourse这个方法
        for(Classes obj: classesList){
            //取当前班  给定课程的 成绩分析数据
            Map  map  = getReportByClassAndCourse(obj.getId(), courseId);
            map.put("className",obj.getName());  //追加班级名称
            result.add(map);  //加入到数组中
        }
        return result;
    }

}
